//
//  WKCountdownButton.m
//  tingche
//
//  Created by 尤振宇 on 2017/6/26.
//  Copyright © 2017年 尤振宇. All rights reserved.
//

#import "WKCountdownButton.h"

@interface WKCountdownButton ()
@property(nonatomic,strong)NSTimer *timer;
@property(nonatomic,assign)NSInteger time;

@end

@implementation WKCountdownButton

-(void)start
{
    [self setEnabled:NO];
    self.time = 60;
    [self setTitle:[NSString stringWithFormat:@"%lds",self.time] forState:(UIControlStateDisabled)];
    [self.timer fire];
}

-(void)stop
{
    [self.timer invalidate];
    self.timer = nil;
    [self setEnabled:YES];
}

-(void)timing
{
    [self setTitle:[NSString stringWithFormat:@"%lds",--self.time] forState:(UIControlStateDisabled)];
    if (self.time == 0) {
        [self stop];
    }
}

-(NSTimer *)timer
{
    if (!_timer) {
        _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timing) userInfo:nil repeats:YES];
    }
    return _timer;
}

@end
